/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sip;

import java.awt.CardLayout;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.MutableComboBoxModel;
import java.util.ArrayList;
import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;
import javax.swing.JFormattedTextField;
import java.util.List;
//Testing
//import javax.swing.Timer;

/**
 *
 * @author arianareyessantiago
 */

public class SimpleInsulinPumpUI extends javax.swing.JFrame {

    /**
     * Creates new form SimpleInsulinPumpUI
     */
    //Variables
    // basal activation status
    boolean basalActive = false;
    // counter to keep check insulin values for how many hours have been entered while creating Basal profile
    public static int hourCounter =0;
    public static int count =0;
    public static boolean basalThreadCount =false;
    // to store the currentBasalProfile being created
    public static BasalProfile currentInstance;
    public double[] insulinPerHour; 
    private String hFileName;
    BufferedReader historyFile;
    String[] timeFrame = {"0:00 To 1:00","1:00 To 2:00","2:00 To 3:00",
"3:00 To 4:00","4:00 To 5:00","5:00 To 6:00","6:00 To 7:00"
,"7:00 To 8:00","8:00 To 9:00","9:00 To 10:00","10:00 To 11:00","11:00 To 12:00",
"12:00 To 13:00","13:00 To 14:00","14:00 To 15:00","15:00 To 16:00","16:00 To 17:00",
"17:00 To 18:00","18:00 To 19:00","19:00 To 20:00","20:00 To 21:00",
"21:00 To 22:00","22:00 To 23:00","23:00 To 24:00"};
    
    //Threads:
    CheckBatteryThread checkBatteryRunnable;
    Thread checkBatteryThread; // = new Thread(checkBatteryRunnable);
    CheckInsulinThread checkInsulinRunnable;
    Thread checkInsulinThread;
    CheckBubblesThread checkBubblesRunnable;
    Thread checkBubblesThread;
    BolusInsulinRunnableNew bolusInsulinRunnable;
    Thread bolusInsulinThread;
    //BasalInsulinRunnable basalInsulinRunnable = new BasalInsulinRunnable();
    //Thread basalThread = new Thread(basalInsulinRunnable);
    BasalInsulinRunnable basalInsulinRunnable ;
    Thread basalThread;
    private BasalProfileFactory mBasalProfileManager;
    CheckStatus check;
    

    public SimpleInsulinPumpUI() {
        
        SipLogger.Instance().debug("Initializing the Simple Insulin Pump");
        //initComponents();
        //battery.startUsingBattery();
        initComponents();
        check = new CheckStatus();
        count = 0;
        basalThreadCount = false;
        currentInstance = new BasalProfile();
        insulinPerHour = new double[24];
        hFileName = "HistoryLog.txt";
        
        
        mBasalProfileManager = new BasalProfileFactory();
        
        
        // code to read basal profile names from file
        
        mBasalProfileManager.readBasalProfiles();
        
  
        
        //System.out.println(" I have passed the readBasalProfile");
        List<String> profileNames = new ArrayList();
        profileNames = mBasalProfileManager.getBasalProfileNames(); 
        String[] profileNamesAsStrings = new String[profileNames.size()];
        for(int i=0 ; i< profileNames.size();i++ )
        {
            profileNamesAsStrings[i] = profileNames.get(i);
        }
        
       // String[] profileNames = mBasalProfileManager.getBasalProfileNames(); 
        //System.out.println(" I have passed the profiles Names");
        profileNameComboBox.setModel(new javax.swing.DefaultComboBoxModel(profileNamesAsStrings));
        //System.out.println(" I have passed the profileNameComboBox");
        //System.out.println(InitSip.totalInsulinAmount);
        if(InitSip.totalInsulinAmount > 1){
            int response = check.checkActiveProfile();
            if(response == 0){
                BasalProfile basalProfileToActivate = mBasalProfileManager.getBasalProfile(InitSip.mActiveProfileName);
                basalInsulinRunnable = new BasalInsulinRunnable();
                basalInsulinRunnable.basalInsulinRunnable(basalProfileToActivate);
                basalThread = new Thread(basalInsulinRunnable);
                basalThread.start();
                activeBasalTextField.setText(InitSip.mActiveProfileName);
                SipLogger.Instance().info("Following Basal Profile has been activated: " + InitSip.mActiveProfileName);
                SipLogger.Instance().infoH("Following Basal Profile has been activated: " + InitSip.mActiveProfileName);
            }
        }
        
        checkBatteryRunnable = new CheckBatteryThread();
        checkBatteryThread = new Thread(checkBatteryRunnable);
        checkBatteryThread.start();
        SipLogger.Instance().debug("CheckBatteryThread Alive: " + Boolean.toString(checkBatteryThread.isAlive()));
        checkBubblesRunnable = new CheckBubblesThread();
        checkBubblesThread = new Thread(checkBubblesRunnable);
        checkBubblesThread.start();
        SipLogger.Instance().debug("CheckBubblesThread Alive: " + Boolean.toString(checkBubblesThread.isAlive()));
        checkInsulinRunnable = new CheckInsulinThread();
        checkInsulinThread = new Thread(checkInsulinRunnable);
        checkInsulinThread.start();
        SipLogger.Instance().debug("CheckInsulinThread Alive: " + Boolean.toString(checkInsulinThread.isAlive()));
    }
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        mainScreen = new javax.swing.JPanel();
        basalButton = new javax.swing.JButton();
        bolusButton = new javax.swing.JButton();
        historyButton = new javax.swing.JButton();
        mainMenuLabel = new javax.swing.JLabel();
        testButton = new javax.swing.JButton();
        bolusScreen = new javax.swing.JPanel();
        labelBolus = new javax.swing.JLabel();
        foodComboBox = new javax.swing.JComboBox();
        subFoodComboBox = new javax.swing.JComboBox();
        foodLabel = new javax.swing.JLabel();
        totalTextField = new javax.swing.JTextField();
        totalLabel = new javax.swing.JLabel();
        addButton2 = new javax.swing.JButton();
        addButton1 = new javax.swing.JButton();
        carbLabel = new javax.swing.JLabel();
        backBolusButton = new javax.swing.JButton();
        bolusScrollPane = new javax.swing.JScrollPane();
        bolusTextArea = new javax.swing.JTextArea();
        gLabel = new javax.swing.JLabel();
        okBolusButton = new javax.swing.JButton();
        carbInputSpinner = new javax.swing.JSpinner();
        clearBolusButton = new javax.swing.JButton();
        insertInsulin = new javax.swing.JButton();
        totalInsulinLabel = new javax.swing.JLabel();
        totalInsulinTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cancelBolusButton = new javax.swing.JButton();
        basalActivationScreen = new javax.swing.JPanel();
        basalActivationLabel = new javax.swing.JLabel();
        profileNameComboBox = new javax.swing.JComboBox();
        profileNameBasalActivationLabel = new javax.swing.JLabel();
        activateBasalActivationButton = new javax.swing.JButton();
        backBasalActivationButton = new javax.swing.JButton();
        addProfileButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        basalScreen = new javax.swing.JPanel();
        basalLabel = new javax.swing.JLabel();
        unitsOfInsulinLabel = new javax.swing.JLabel();
        profileNameTextField = new javax.swing.JTextField();
        profileNameLabel = new javax.swing.JLabel();
        backBasalButton = new javax.swing.JButton();
        saveBasalButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        timeFrameLabel = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        Double[] insulinAmount = {0.00,0.25,0.50,0.75,1.0,1.25,1.50,1.75,
            2.0,2.25,2.50,2.75,3.0,3.25,3.50,3.75,4.0}; //get month names
        SpinnerListModel insulinAmountModel = new SpinnerListModel(insulinAmount);
        insulinAmountSpinner = new javax.swing.JSpinner(insulinAmountModel);
        jLabel6 = new javax.swing.JLabel();
        verificationScreen = new javax.swing.JPanel();
        pinLabel = new javax.swing.JLabel();
        okVerificationButton = new javax.swing.JButton();
        backVerificationButton = new javax.swing.JButton();
        verificationPassword = new javax.swing.JPasswordField();
        incorrectMessageLabel = new javax.swing.JLabel();
        historyScreen = new javax.swing.JPanel();
        historyScrollPane = new javax.swing.JScrollPane();
        historyTextArea = new javax.swing.JTextArea();
        historyLabel = new javax.swing.JLabel();
        backHistoryButton = new javax.swing.JButton();
        testScreen = new javax.swing.JPanel();
        backTestButton = new javax.swing.JButton();
        testLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        insulinTestTextField = new javax.swing.JTextField();
        batteryTestTextField = new javax.swing.JTextField();
        bubblesTestTextField = new javax.swing.JTextField();
        setInsulinButton = new javax.swing.JButton();
        setBatteryButton = new javax.swing.JButton();
        setBubblesButton = new javax.swing.JButton();
        upperPanel = new javax.swing.JPanel();
        insulinLabel = new javax.swing.JLabel();
        timeLabel = new sip.ClockLabel();
        insulinTextField = new InsulinLabel();
        batteryLabel = new sip.BatteryLabel();
        jLabel5 = new javax.swing.JLabel();
        lowerPanel = new javax.swing.JPanel();
        activeBasalLabel = new javax.swing.JLabel();
        stopButton = new javax.swing.JButton();
        pauseButton = new javax.swing.JButton();
        activeBasalTextField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        mainPanel.setLayout(new java.awt.CardLayout());

        mainScreen.setBackground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));
        mainScreen.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        mainScreen.setForeground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));
        mainScreen.setPreferredSize(new java.awt.Dimension(500, 240));

        basalButton.setBackground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));
        basalButton.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        basalButton.setText("Basal ");
        basalButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        basalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                basalButtonActionPerformed(evt);
            }
        });

        bolusButton.setBackground(javax.swing.UIManager.getDefaults().getColor("CheckBoxMenuItem.selectionBackground"));
        bolusButton.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        bolusButton.setText("Bolus");
        bolusButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        bolusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bolusButtonActionPerformed(evt);
            }
        });
        bolusButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                bolusButtonKeyPressed(evt);
            }
        });

        historyButton.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        historyButton.setText("History");
        historyButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        historyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                historyButtonActionPerformed(evt);
            }
        });

        mainMenuLabel.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        mainMenuLabel.setText("Main Menu");

        testButton.setBackground(javax.swing.UIManager.getDefaults().getColor("CheckBoxMenuItem.selectionBackground"));
        testButton.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        testButton.setText("Test");
        testButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        testButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testButtonActionPerformed(evt);
            }
        });
        testButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                testButtonKeyPressed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout mainScreenLayout = new org.jdesktop.layout.GroupLayout(mainScreen);
        mainScreen.setLayout(mainScreenLayout);
        mainScreenLayout.setHorizontalGroup(
            mainScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainScreenLayout.createSequentialGroup()
                .addContainerGap()
                .add(mainScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(mainScreenLayout.createSequentialGroup()
                        .add(0, 443, Short.MAX_VALUE)
                        .add(mainMenuLabel)
                        .addContainerGap())
                    .add(mainScreenLayout.createSequentialGroup()
                        .add(mainScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, testButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, bolusButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, basalButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, historyButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE))
                        .add(0, 0, Short.MAX_VALUE))))
        );
        mainScreenLayout.setVerticalGroup(
            mainScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainScreenLayout.createSequentialGroup()
                .addContainerGap()
                .add(mainMenuLabel)
                .add(23, 23, 23)
                .add(basalButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(bolusButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(historyButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(testButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(83, Short.MAX_VALUE))
        );

        mainPanel.add(mainScreen, "mainScreen");

        bolusScreen.setBackground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));
        bolusScreen.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        bolusScreen.setPreferredSize(new java.awt.Dimension(500, 240));

        labelBolus.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        labelBolus.setText("BOLUS");

        foodComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String [] {"Select", "Breakfast", "Meat", "Fish", "Snacks", "Juices"}));
        foodComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                foodComboBoxActionPerformed(evt);
            }
        });

        subFoodComboBox.setVerifyInputWhenFocusTarget(false);

        foodLabel.setText("Food");

        totalTextField.setEditable(false);
        totalTextField.setText("0.00");
        totalTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                totalTextFieldActionPerformed(evt);
            }
        });

        totalLabel.setText("Total");

        addButton2.setText("ADD");
        addButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButton2ActionPerformed(evt);
            }
        });

        addButton1.setText("ADD");
        addButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButton1ActionPerformed(evt);
            }
        });

        carbLabel.setText("Carbs");

        backBolusButton.setText("Back");
        backBolusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backBolusButtonActionPerformed(evt);
            }
        });

        bolusTextArea.setColumns(20);
        bolusTextArea.setRows(5);
        bolusTextArea.setEditable(false);
        bolusScrollPane.setViewportView(bolusTextArea);

        gLabel.setText("g");

        okBolusButton.setText("OK");
        okBolusButton.setPreferredSize(new java.awt.Dimension(76, 29));
        okBolusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okBolusButtonActionPerformed(evt);
            }
        });

        carbInputSpinner.setModel(new javax.swing.SpinnerNumberModel(0.0d, -10.0d, 50.0d, 1.0d));

        clearBolusButton.setText("Clear");
        clearBolusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearBolusButtonActionPerformed(evt);
            }
        });

        insertInsulin.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        insertInsulin.setText("Insert Bolus");
        insertInsulin.setPreferredSize(new java.awt.Dimension(76, 29));
        insertInsulin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertInsulinActionPerformed(evt);
            }
        });

        totalInsulinLabel.setText("Total Insulin");

        totalInsulinTextField.setText("0.00");
        totalInsulinTextField.setEditable(false);
        totalInsulinTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                totalInsulinTextFieldActionPerformed(evt);
            }
        });

        jLabel2.setText("U");

        cancelBolusButton.setText("Cancel");
        cancelBolusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBolusButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout bolusScreenLayout = new org.jdesktop.layout.GroupLayout(bolusScreen);
        bolusScreen.setLayout(bolusScreenLayout);
        bolusScreenLayout.setHorizontalGroup(
            bolusScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(bolusScreenLayout.createSequentialGroup()
                .addContainerGap()
                .add(bolusScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(bolusScreenLayout.createSequentialGroup()
                        .add(bolusScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(foodLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 31, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(carbLabel)
                            .add(totalLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(bolusScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(bolusScreenLayout.createSequentialGroup()
                                .add(bolusScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(bolusScreenLayout.createSequentialGroup()
                                        .add(carbInputSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 68, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(bolusScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(bolusScreenLayout.createSequentialGroup()
                                                .add(34, 34, 34)
                                                .add(subFoodComboBox, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                            .add(bolusScreenLayout.createSequentialGroup()
                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                .add(addButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 54, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                .add(0, 0, Short.MAX_VALUE))))
                                    .add(bolusScreenLayout.createSequentialGroup()
                                        .add(foodComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 102, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(0, 0, Short.MAX_VALUE)))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(addButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 54, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(65, 65, 65))
                            .add(bolusScreenLayout.createSequentialGroup()
                                .add(totalTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 67, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(gLabel)
                                .add(24, 24, 24)
                                .add(totalInsulinLabel)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(totalInsulinTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 91, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel2)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(insertInsulin, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 131, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())))
                    .add(bolusScreenLayout.createSequentialGroup()
                        .add(bolusScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(bolusScreenLayout.createSequentialGroup()
                                .add(backBolusButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 63, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(labelBolus))
                            .add(bolusScreenLayout.createSequentialGroup()
                                .add(bolusScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 440, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(bolusScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(bolusScreenLayout.createSequentialGroup()
                                        .add(0, 0, Short.MAX_VALUE)
                                        .add(bolusScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(org.jdesktop.layout.GroupLayout.TRAILING, clearBolusButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 63, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                            .add(org.jdesktop.layout.GroupLayout.TRAILING, okBolusButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 58, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                    .add(bolusScreenLayout.createSequentialGroup()
                                        .add(cancelBolusButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 72, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .add(0, 4, Short.MAX_VALUE)))))
                        .addContainerGap())))
        );
        bolusScreenLayout.setVerticalGroup(
            bolusScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(bolusScreenLayout.createSequentialGroup()
                .addContainerGap()
                .add(bolusScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(backBolusButton)
                    .add(labelBolus))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(bolusScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(addButton1)
                    .add(carbLabel)
                    .add(carbInputSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(bolusScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(foodLabel)
                    .add(foodComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(subFoodComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(addButton2))
                .add(3, 3, 3)
                .add(bolusScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(totalLabel)
                    .add(totalTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(gLabel)
                    .add(insertInsulin, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(totalInsulinLabel)
                    .add(totalInsulinTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2))
                .add(10, 10, 10)
                .add(bolusScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(bolusScreenLayout.createSequentialGroup()
                        .add(cancelBolusButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 41, Short.MAX_VALUE)
                        .add(clearBolusButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(okBolusButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(bolusScrollPane))
                .addContainerGap())
        );

        mainPanel.add(bolusScreen, "bolusScreen");

        basalActivationScreen.setBackground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));
        basalActivationScreen.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        basalActivationScreen.setName("basalActivationScreen"); // NOI18N
        basalActivationScreen.setPreferredSize(new java.awt.Dimension(500, 240));

        basalActivationLabel.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        basalActivationLabel.setText(" BASAL ACTIVATION ");

        profileNameComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        profileNameComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                profileNameComboBoxActionPerformed(evt);
            }
        });

        profileNameBasalActivationLabel.setText("Profile Name");

        activateBasalActivationButton.setText("Activate");
        activateBasalActivationButton.setName("Activate"); // NOI18N
        activateBasalActivationButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                activateBasalActivationButtonActionPerformed(evt);
            }
        });

        backBasalActivationButton.setText("Back");
        backBasalActivationButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backBasalActivationButtonActionPerformed(evt);
            }
        });

        addProfileButton.setText("Add Profile");
        addProfileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addProfileButtonActionPerformed(evt);
            }
        });

        deleteButton.setText("Delete Selected Profile");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout basalActivationScreenLayout = new org.jdesktop.layout.GroupLayout(basalActivationScreen);
        basalActivationScreen.setLayout(basalActivationScreenLayout);
        basalActivationScreenLayout.setHorizontalGroup(
            basalActivationScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(basalActivationScreenLayout.createSequentialGroup()
                .addContainerGap()
                .add(basalActivationScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, basalActivationScreenLayout.createSequentialGroup()
                        .add(profileNameBasalActivationLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(profileNameComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 207, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(0, 0, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, basalActivationScreenLayout.createSequentialGroup()
                        .add(basalActivationScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(backBasalActivationButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 63, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(basalActivationScreenLayout.createSequentialGroup()
                                .add(activateBasalActivationButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 133, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(18, 18, 18)
                                .add(deleteButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)))
                        .add(12, 12, 12)
                        .add(basalActivationScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(basalActivationLabel)
                            .add(addProfileButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        basalActivationScreenLayout.setVerticalGroup(
            basalActivationScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(basalActivationScreenLayout.createSequentialGroup()
                .addContainerGap()
                .add(basalActivationScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(backBasalActivationButton)
                    .add(basalActivationLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(basalActivationScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(profileNameBasalActivationLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 45, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(profileNameComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 45, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(45, 45, 45)
                .add(basalActivationScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addProfileButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 43, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(activateBasalActivationButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 43, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(deleteButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 43, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(65, Short.MAX_VALUE))
        );

        mainPanel.add(basalActivationScreen, "basalActivationScreen");

        basalScreen.setBackground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));
        basalScreen.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        basalScreen.setName("basalScreen"); // NOI18N

        basalLabel.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        basalLabel.setText("BASAL");

        unitsOfInsulinLabel.setText("Units of Insulin");

        profileNameTextField.setText("");
        profileNameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                profileNameTextFieldActionPerformed(evt);
            }
        });

        profileNameLabel.setText("Profile Name");

        backBasalButton.setText("Back");
        backBasalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backBasalButtonActionPerformed(evt);
            }
        });

        saveBasalButton.setLabel("Save");
        saveBasalButton.setName("Save"); // NOI18N
        saveBasalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBasalButtonActionPerformed(evt);
            }
        });

        nextButton.setText("Enter & Next");
        nextButton.setName("Next"); // NOI18N
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });

        timeFrameLabel.setText("Time Frame");

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        JFormattedTextField ampmspin=((JSpinner.DefaultEditor)insulinAmountSpinner.getEditor()).getTextField();
        ampmspin.setEditable(false);

        jLabel6.setText("jLabel6");

        org.jdesktop.layout.GroupLayout basalScreenLayout = new org.jdesktop.layout.GroupLayout(basalScreen);
        basalScreen.setLayout(basalScreenLayout);
        basalScreenLayout.setHorizontalGroup(
            basalScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(basalScreenLayout.createSequentialGroup()
                .add(basalScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(basalScreenLayout.createSequentialGroup()
                        .addContainerGap()
                        .add(backBasalButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 63, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(basalLabel))
                    .add(basalScreenLayout.createSequentialGroup()
                        .add(95, 95, 95)
                        .add(saveBasalButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 144, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(45, 45, 45)
                        .add(cancelButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 149, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(0, 89, Short.MAX_VALUE)))
                .addContainerGap())
            .add(org.jdesktop.layout.GroupLayout.TRAILING, basalScreenLayout.createSequentialGroup()
                .addContainerGap(71, Short.MAX_VALUE)
                .add(basalScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(profileNameLabel)
                    .add(unitsOfInsulinLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(35, 35, 35)
                .add(basalScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(profileNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 181, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(basalScreenLayout.createSequentialGroup()
                        .add(basalScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(basalScreenLayout.createSequentialGroup()
                                .add(insulinAmountSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 72, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(timeFrameLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 73, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(basalScreenLayout.createSequentialGroup()
                                .add(13, 13, 13)
                                .add(nextButton)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 114, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(51, 51, 51))
        );
        basalScreenLayout.setVerticalGroup(
            basalScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(basalScreenLayout.createSequentialGroup()
                .addContainerGap()
                .add(basalScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(backBasalButton)
                    .add(basalLabel))
                .add(basalScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(profileNameLabel)
                    .add(profileNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(basalScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(unitsOfInsulinLabel)
                    .add(insulinAmountSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(timeFrameLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(41, 41, 41)
                .add(nextButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 45, Short.MAX_VALUE)
                .add(basalScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(saveBasalButton)
                    .add(cancelButton))
                .add(42, 42, 42))
        );

        mainPanel.add(basalScreen, "basalScreen");

        verificationScreen.setBackground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));
        verificationScreen.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        verificationScreen.setForeground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));
        verificationScreen.setPreferredSize(new java.awt.Dimension(500, 240));

        pinLabel.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        pinLabel.setText("Enter PIN");

        okVerificationButton.setText("OK");
        okVerificationButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okVerificationButtonActionPerformed(evt);
            }
        });

        backVerificationButton.setText("Back");
        backVerificationButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backVerificationButtonActionPerformed(evt);
            }
        });

        incorrectMessageLabel.setBackground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));
        incorrectMessageLabel.setFont(new java.awt.Font("Lucida Grande", 0, 14)); // NOI18N
        incorrectMessageLabel.setText("");

        org.jdesktop.layout.GroupLayout verificationScreenLayout = new org.jdesktop.layout.GroupLayout(verificationScreen);
        verificationScreen.setLayout(verificationScreenLayout);
        verificationScreenLayout.setHorizontalGroup(
            verificationScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(verificationScreenLayout.createSequentialGroup()
                .addContainerGap()
                .add(verificationScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(pinLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 87, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(verificationScreenLayout.createSequentialGroup()
                        .add(verificationPassword, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 126, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(okVerificationButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 53, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(backVerificationButton)
                    .add(incorrectMessageLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 332, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(160, Short.MAX_VALUE))
        );
        verificationScreenLayout.setVerticalGroup(
            verificationScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(verificationScreenLayout.createSequentialGroup()
                .addContainerGap()
                .add(backVerificationButton)
                .add(29, 29, 29)
                .add(pinLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 31, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(7, 7, 7)
                .add(verificationScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(verificationPassword, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(okVerificationButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(incorrectMessageLabel)
                .addContainerGap(83, Short.MAX_VALUE))
        );

        mainPanel.add(verificationScreen, "verificationScreen");

        historyScreen.setBackground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));
        historyScreen.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        historyScreen.setPreferredSize(new java.awt.Dimension(500, 240));

        historyTextArea.setColumns(20);
        historyTextArea.setRows(5);
        historyTextArea.setEditable(false);
        historyScrollPane.setViewportView(historyTextArea);

        historyLabel.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        historyLabel.setText("History");

        backHistoryButton.setText("Back");
        backHistoryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backHistoryButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout historyScreenLayout = new org.jdesktop.layout.GroupLayout(historyScreen);
        historyScreen.setLayout(historyScreenLayout);
        historyScreenLayout.setHorizontalGroup(
            historyScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(historyScreenLayout.createSequentialGroup()
                .add(historyScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(historyScreenLayout.createSequentialGroup()
                        .add(backHistoryButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(historyLabel))
                    .add(historyScreenLayout.createSequentialGroup()
                        .addContainerGap()
                        .add(historyScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)))
                .addContainerGap())
        );
        historyScreenLayout.setVerticalGroup(
            historyScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, historyScreenLayout.createSequentialGroup()
                .addContainerGap()
                .add(historyScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(historyLabel)
                    .add(backHistoryButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(historyScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                .addContainerGap())
        );

        mainPanel.add(historyScreen, "historyScreen");

        testScreen.setBackground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));
        testScreen.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        testScreen.setPreferredSize(new java.awt.Dimension(500, 240));

        backTestButton.setText("Back");
        backTestButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backTestButtonActionPerformed(evt);
            }
        });

        testLabel.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        testLabel.setText("Test");

        jLabel1.setText("Insulin Amount");

        jLabel3.setText("Battery Level (%)");

        jLabel4.setText("Bubbles (true or false)");

        insulinTestTextField.setText("");
        insulinTestTextField.setPreferredSize(new java.awt.Dimension(100, 28));

        batteryTestTextField.setText("");
        batteryTestTextField.setPreferredSize(new java.awt.Dimension(100, 28));

        bubblesTestTextField.setText("");
        bubblesTestTextField.setPreferredSize(new java.awt.Dimension(100, 28));

        setInsulinButton.setText("Set");
        setInsulinButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setInsulinButtonActionPerformed(evt);
            }
        });

        setBatteryButton.setText("Set");
        setBatteryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setBatteryButtonActionPerformed(evt);
            }
        });

        setBubblesButton.setText("Set");
        setBubblesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setBubblesButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout testScreenLayout = new org.jdesktop.layout.GroupLayout(testScreen);
        testScreen.setLayout(testScreenLayout);
        testScreenLayout.setHorizontalGroup(
            testScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(testScreenLayout.createSequentialGroup()
                .addContainerGap()
                .add(testScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(testScreenLayout.createSequentialGroup()
                        .add(backTestButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(testLabel))
                    .add(testScreenLayout.createSequentialGroup()
                        .add(testScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(testScreenLayout.createSequentialGroup()
                                .add(jLabel4)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(bubblesTestTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(setBubblesButton))
                            .add(testScreenLayout.createSequentialGroup()
                                .add(jLabel1)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(insulinTestTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(setInsulinButton))
                            .add(testScreenLayout.createSequentialGroup()
                                .add(jLabel3)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(batteryTestTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(setBatteryButton)))
                        .add(0, 198, Short.MAX_VALUE)))
                .addContainerGap())
        );
        testScreenLayout.setVerticalGroup(
            testScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(testScreenLayout.createSequentialGroup()
                .addContainerGap()
                .add(testScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(backTestButton)
                    .add(testLabel))
                .add(18, 18, 18)
                .add(testScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(insulinTestTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(setInsulinButton))
                .add(18, 18, 18)
                .add(testScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(batteryTestTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(setBatteryButton))
                .add(18, 18, 18)
                .add(testScreenLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(bubblesTestTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(setBubblesButton))
                .addContainerGap(115, Short.MAX_VALUE))
        );

        mainPanel.add(testScreen, "testScreen");

        upperPanel.setBackground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));

        insulinLabel.setText("Reservoir:");

        timeLabel.setText("");

        insulinTextField.setEditable(false);
        Bolus b = new Bolus();
        insulinTextField.setText(Double.toString(InitSip.totalInsulinAmount));
        insulinTextField.setEditable(false);
        insulinTextField.setMinimumSize(new java.awt.Dimension(10, 28));
        insulinTextField.setPreferredSize(new java.awt.Dimension(10, 28));
        insulinTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insulinTextFieldActionPerformed(evt);
            }
        });

        batteryLabel.setText(Double.toString(InitSip.BatteryLevel) + "%");

        jLabel5.setText("units");

        org.jdesktop.layout.GroupLayout upperPanelLayout = new org.jdesktop.layout.GroupLayout(upperPanel);
        upperPanel.setLayout(upperPanelLayout);
        upperPanelLayout.setHorizontalGroup(
            upperPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, upperPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(batteryLabel)
                .add(37, 37, 37)
                .add(insulinLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(insulinTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 83, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel5)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 170, Short.MAX_VALUE)
                .add(timeLabel)
                .addContainerGap())
        );
        upperPanelLayout.setVerticalGroup(
            upperPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(upperPanelLayout.createSequentialGroup()
                .add(6, 6, 6)
                .add(upperPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(insulinLabel)
                    .add(timeLabel)
                    .add(insulinTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(batteryLabel)
                    .add(jLabel5))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        lowerPanel.setBackground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));

        activeBasalLabel.setText("Active Basal:");

        stopButton.setText("STOP");
        stopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopButtonActionPerformed(evt);
            }
        });

        pauseButton.setText("PAUSE");
        pauseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pauseButtonActionPerformed(evt);
            }
        });

        activeBasalTextField.setText("");
        activeBasalTextField.setPreferredSize(new java.awt.Dimension(100, 28));

        org.jdesktop.layout.GroupLayout lowerPanelLayout = new org.jdesktop.layout.GroupLayout(lowerPanel);
        lowerPanel.setLayout(lowerPanelLayout);
        lowerPanelLayout.setHorizontalGroup(
            lowerPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, lowerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(stopButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(pauseButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(activeBasalLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(activeBasalTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(22, 22, 22))
        );
        lowerPanelLayout.setVerticalGroup(
            lowerPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(lowerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(lowerPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(activeBasalLabel)
                    .add(stopButton)
                    .add(pauseButton)
                    .add(activeBasalTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(0, 9, Short.MAX_VALUE)
                .add(mainPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(0, 0, Short.MAX_VALUE))
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, lowerPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(upperPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(upperPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(mainPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(lowerPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    /**
     * @param args the command line arguments
     */
    private void bolusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bolusButtonActionPerformed

        foodComboBox.setSelectedIndex(-1);
        subFoodComboBox.setSelectedIndex(-1);
        
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel, "bolusScreen");//(mainPanel, "bolusScreen");
        
    }//GEN-LAST:event_bolusButtonActionPerformed

    private void bolusButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_bolusButtonKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_bolusButtonKeyPressed

    private void backBolusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backBolusButtonActionPerformed

        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel, "mainScreen");//(mainPanel, "mainScreen")
        insertInsulin.setEnabled(true);
        totalTextField.setText("0.00");
        totalInsulinTextField.setText("0.00");

    }//GEN-LAST:event_backBolusButtonActionPerformed

    private void addButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButton2ActionPerformed
        // TODO add your handling code here:
        
        Bolus bolus = new Bolus();
        //Extracts the cabValue of the Selected Item and calculates the new total of carbs
        String itemSelected = (String) subFoodComboBox.getSelectedItem();
        bolusTextArea.append(itemSelected + "\n");
        Double carbValue = new Double(itemSelected.split(" g|-- ")[1]);
        //System.out.println(carbValue);//test
        Double currentTotalCarbs = new Double(String.valueOf(totalTextField.getText()));
        Double totalCarbs = currentTotalCarbs + carbValue;
        //System.out.println(totalCarbs); //test
        totalTextField.setText(Double.toString(totalCarbs));
        
        //Gets the insulin amount per total of carbs and calculates the new total of Insulin
        //Double currentInsulinUnits = new Double(String.valueOf(totalInsulinTextField.getText()));
        bolus.totalInsulin(totalCarbs);
        
        totalInsulinTextField.setText(Double.toString(Bolus.bolusInsulin));
        
    }//GEN-LAST:event_addButton2ActionPerformed

    private void foodComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_foodComboBoxActionPerformed
        // TODO add your handling code here:
        Bolus bolus = new Bolus();
        String itemOption = (String) foodComboBox.getSelectedItem();
        String [] subList = bolus.chooseSubList(itemOption);
        subFoodComboBox.setModel(new javax.swing.DefaultComboBoxModel(subList));

    }//GEN-LAST:event_foodComboBoxActionPerformed

    private void profileNameComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_profileNameComboBoxActionPerformed
        // TODO add your handling code here:

    }//GEN-LAST:event_profileNameComboBoxActionPerformed

    private void activateBasalActivationButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_activateBasalActivationButtonActionPerformed
           // TODO add your handling code here:
        
        // checks if there is any profile present to activate
       // String state  = basalThread.getState().toString();
       // if(state != "NEW" )
       // {
        //}
        if(profileNameComboBox.getItemCount() ==0)
        {
             Alarm alarm = new Alarm ();
            alarm.setAlarm("Invalid Basal Profile selected"); 
        }
        else
        {
             Object selectedItem= profileNameComboBox.getSelectedItem();
             String test = (String)selectedItem;
            if(test.isEmpty() )
            {
                Alarm alarm = new Alarm ();
                alarm.setAlarm("Invalid Basal Profile selected"); 
            }
            else
        {
        InitSip initSip = new InitSip();
        Object count = profileNameComboBox.getItemAt(0);
        
        if(count == null )
        {
            JOptionPane.showMessageDialog(null,"There are no profiles to activate", "ERROR !!!!!!" , JOptionPane.ERROR_MESSAGE);
        }
        else
        {   
            //ask for user confirmation
           Alarm a= new Alarm();
            int result = a.confirm("Are you sure you want to activate this Basal Profile");
            
            
            if(result == 0)
            {    
                    
                    String selectedProfile = profileNameComboBox.getSelectedItem().toString();
                    mBasalProfileManager.activateProfile(mBasalProfileManager.getBasalProfile(selectedProfile));
                    InitSip.mActiveProfileName = selectedProfile;
                    initSip.writeCurrentValues();
                    activeBasalTextField.setText(InitSip.mActiveProfileName);
                    SipLogger.Instance().info("The following Profile has been activated: " + selectedProfile);
                    SipLogger.Instance().infoH("The following Profile has been activated: " + selectedProfile);
                    //
                    BasalProfile basalProfileToActivate = mBasalProfileManager.getBasalProfile(selectedProfile);
                    
                    //basalThread = new Thread(basalInsulin);
                    //check if the thread is new starts it
                    
                    // else resume with new basalProfile
                    
                    if(basalThread != null)
        {
                        if(basalThread.isAlive())
                            //basalInsulinRunnable.
                        basalInsulinRunnable.terminate();
                        try{
                              basalThread.join();
                          }catch(InterruptedException e){
                              SipLogger.Instance().debug("basalThread was iterrupted.");
                          }
                    
                       // basalInsulinRunnable_1 = new BasalInsulinRunnable();
                       // basalInsulinRunnable_1.basalInsulinRunnable(basalProfileToActivate);
                        //basalThread_1 = new Thread(basalInsulinRunnable);
                        basalInsulinRunnable.resumeThread();
                        
        }
            
            
                        
                        
                        basalInsulinRunnable = new BasalInsulinRunnable();
                        basalInsulinRunnable.basalInsulinRunnable(basalProfileToActivate);
                        basalThread = new Thread(basalInsulinRunnable);
                        basalThread.start();
                  
                }
               }
        basalThreadCount = true;
        basalActive = true;
        }
        }
        
    }//GEN-LAST:event_activateBasalActivationButtonActionPerformed

    private void backBasalActivationButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backBasalActivationButtonActionPerformed
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel,"mainScreen");// card2 + mainScreen
    }//GEN-LAST:event_backBasalActivationButtonActionPerformed

    private void addProfileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addProfileButtonActionPerformed
        // TODO add your handling code here:
        
        // clear everything
        saveBasalButton.setEnabled(true);
        
        profileNameTextField.setText("");
      // Double firstValue = insulinAmount[0];
       // insulinAmountSpinner.setValue();
        jLabel6.setText(timeFrame[0]);
        nextButton.setText("Enter & Next");
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel,"basalScreen");
        
       
    }//GEN-LAST:event_addProfileButtonActionPerformed

    private void basalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_basalButtonActionPerformed
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel, "verificationScreen");
    }//GEN-LAST:event_basalButtonActionPerformed

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
        // TODO add your handling code here:
        hourCounter++;
        if(count == 24)
        {
            Alarm alarm = new Alarm();
            alarm.setAlarm("You have already entered Insulin amount for 24 Hours");
        }  
        else
        {
        // saves the basal value into an array using BasalProfile class
        String basalValue = String.valueOf(insulinAmountSpinner.getValue());
        Double basalValueDouble = new Double(basalValue);
        //saves the selected insulin value
        insulinPerHour[count]= basalValueDouble;
        if(count==22)
        {
             nextButton.setText("Enter");
             
        }
        if(count==23)
        {
            nextButton.setEnabled(false);
            jLabel6.setText(timeFrame[count]);
           // timeFrameSpinner.setValue(timeFrame[count]);
           
        }
        else
        {
            jLabel6.setText(timeFrame[count+1]);
            //timeFrameSpinner.setValue(timeFrame[count+1]);
        }
        
            //increments the count
            count++;
        }
    }//GEN-LAST:event_nextButtonActionPerformed

    private void saveBasalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBasalButtonActionPerformed
        // TODO add your handling code here:
        // checks if the profile name field is left empty
        
        if(profileNameTextField.getText().isEmpty())
        {
            Alarm alarm = new Alarm();
            alarm.setAlarm("Basal Profile Name field cannot be left empty");
        }
        // to check if the basal value has been entered for all 24 hours.
        else 
        {
             // to check if the basal profile with same name already exists
          List<String> profileNameAlreadyExisting = new ArrayList();
          profileNameAlreadyExisting = mBasalProfileManager.getBasalProfileNames();
           int i=0;
           boolean alreadyExists= false;
           int size = profileNameAlreadyExisting.size();
           for(i=0;i<size;i++)
           {
               String name = profileNameTextField.getText();
               boolean exists = profileNameAlreadyExisting.get(i).equalsIgnoreCase(name);
               if(exists)
               {
                   alreadyExists = true;
                   break;
               }
               
           }
           if(alreadyExists)
           {
               Alarm alarm = new Alarm();
               alarm.setAlarm("Basal profile with same name already exists");
               //profileNameTextField.setText("");
           }
           else if(hourCounter <24)
             {
                 Alarm alarm = new Alarm();
                 alarm.setAlarm("Insulin value should be entered for 24 hour");
            }
           else
           {
                            // adds the basal profile created to the basal arraylist
                            currentInstance.mProfile = insulinPerHour;
                            currentInstance.setProfileName(profileNameTextField.getText());

                            //displaying confirmation
                            Alarm alarm = new Alarm();
                            int result = alarm.confirm("Are you sure you want to save this Basal Profile?");
                            if(result == 0) // if user presses YES
                            {
                                // profile gets saved to the config file 
                                mBasalProfileManager.addProfile(profileNameTextField.getText(), currentInstance);
                                mBasalProfileManager.saveBasalProfiles();
                            }
                            else
                            {
                                alarm.setAlarm("Profile has not been saved. Please go back and add new profile");
                            }


                            // the profile name combo box get populated with the new saved basal profile
                            mBasalProfileManager.readBasalProfiles();
                            List<String> profileNames = new ArrayList();
                            profileNames = mBasalProfileManager.getBasalProfileNames(); 
                            String[] profileNamesAsStrings = new String[profileNames.size()];
                            for(int j=0 ; j< profileNames.size();j++ )
                             {
                                    profileNamesAsStrings[j] = profileNames.get(j);
                             }
                            //String[] profileNames = mBasalProfileManager.getBasalProfileNames();        
                            profileNameComboBox.setModel(new javax.swing.DefaultComboBoxModel(profileNamesAsStrings));
                            hourCounter = 0;
                            count =0;
                            saveBasalButton.setEnabled(false);
           }
        }
       
    }//GEN-LAST:event_saveBasalButtonActionPerformed

    private void backBasalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backBasalButtonActionPerformed
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel,"basalActivationScreen");
        count =0;
        nextButton.setEnabled(false);
    }//GEN-LAST:event_backBasalButtonActionPerformed

    private void addButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButton1ActionPerformed
        // TODO add your handling code here:
        Bolus bolus = new Bolus();
        Double currentTotalCarbs = new Double(String.valueOf(totalTextField.getText()));
        Double currentInsulinUnits = new Double(String.valueOf(totalInsulinTextField.getText()));
        Double carbValue = new Double(String.valueOf(carbInputSpinner.getValue()));
        double totalCarbs = currentTotalCarbs + carbValue; 
        bolusTextArea.append("Carb Input -- "+ Double.toString(carbValue) +" g\n");
        bolus.totalInsulin(totalCarbs);
        //Bolus.bolusInsulin = currentInsulinUnits + Bolus.bolusInsulin;
        totalInsulinTextField.setText(Double.toString(Bolus.bolusInsulin));
        totalTextField.setText(Double.toString(totalCarbs));
    }//GEN-LAST:event_addButton1ActionPerformed

    private void okVerificationButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okVerificationButtonActionPerformed
        // TODO add your handling code here:
       CardLayout cl = (CardLayout)(mainPanel.getLayout());
       char[] pin;
        pin =  verificationPassword.getPassword();
        String pinString = new String(pin);
        //System.out.println(pin);
       if (pinString.equals("1234")){
           cl.show(mainPanel,"basalActivationScreen");
           verificationPassword.setText("");
           incorrectMessageLabel.setText("");
       }
       else{
           verificationPassword.setText("");
           incorrectMessageLabel.setText("Incorrect Password. Please try again.");
       }
    
    }//GEN-LAST:event_okVerificationButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel,"mainScreen");
        count =0;
        nextButton.setEnabled(true);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void okBolusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okBolusButtonActionPerformed
        // TODO add your handling code here:
        insertInsulin.setEnabled(true);
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel,"mainScreen");
        
    }//GEN-LAST:event_okBolusButtonActionPerformed

    private void historyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_historyButtonActionPerformed
        // TODO add your handling code here:
        
        String lineOfText = "";
        
        File f = new File(hFileName);
        
        if(f.exists())
        {   //reads in buffer form, instead of line by line
            try {
                historyFile = new BufferedReader(new FileReader(hFileName));
                lineOfText = historyFile.readLine();
                while(lineOfText != null){
                    historyTextArea.append(lineOfText + "\n");
                    System.out.println(" This is in the lineOfText: " + lineOfText + "\n");
                    lineOfText = historyFile.readLine();
                }
                
            }catch (IOException e) {
                System.out.println("ERROR:  Exception:  " + e.toString());
            }
    
        }
   
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel, "historyScreen");
        
        
    }//GEN-LAST:event_historyButtonActionPerformed

    private void backHistoryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backHistoryButtonActionPerformed
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel, "mainScreen");
    }//GEN-LAST:event_backHistoryButtonActionPerformed

    private void backVerificationButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backVerificationButtonActionPerformed
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel,"mainScreen");
    }//GEN-LAST:event_backVerificationButtonActionPerformed

    private void totalTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_totalTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_totalTextFieldActionPerformed

    private void clearBolusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearBolusButtonActionPerformed
        // TODO add your handling code here:
        bolusTextArea.setText("");
        totalTextField.setText("0.00");
        totalInsulinTextField.setText("0.00");
    }//GEN-LAST:event_clearBolusButtonActionPerformed

    private void insulinTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insulinTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_insulinTextFieldActionPerformed

    private void insertInsulinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertInsulinActionPerformed
        // TODO add your handling code here:
        //cancelBolusButton.setVisible(true);
        
        Bolus bolus = new Bolus();
        Alarm alarm = new Alarm();
        if(bolusInsulinThread != null){
            if(bolusInsulinThread.isAlive()){
                bolusInsulinRunnable.terminate();
                try{
                    basalThread.join();
                }catch(InterruptedException e){
                    SipLogger.Instance().debug("basalThread was iterrupted.");
                }
            }
        }
        bolusInsulinRunnable = new BolusInsulinRunnableNew();
        bolusInsulinThread = new Thread(bolusInsulinRunnable);
        //Double totalInsulinUnits = new Double(String.valueOf(totalInsulinTextField.getText()));
        
        if(Bolus.bolusInsulin <= 0){
           bolusTextArea.append("You don't have any Insulin to insert!"); 
        }
        else if (InitSip.totalInsulinAmount > Bolus.bolusInsulin){
            
            int res = alarm.confirm("Are you sure you want to insert " + Double.toString(Bolus.bolusInsulin) + " U?");
            
            if(res == 0){
            bolusTextArea.append("Total Insulin -- "+ Double.toString(Bolus.bolusInsulin) +" U\n");
                if(!bolusInsulinThread.isAlive()){
                bolusInsulinThread.start();
                insertInsulin.setEnabled(false);
                
                //
                //bolusInsulinRunnable = new BolusInsulinRunnableNew();
                //System.out.println("I made it after the BolusInsulinRunnable call.");
                //SipLogger.Instance().debug("BolusInsulinThread Alive: " + Boolean.toString(bolusInsulinThread.));
                totalTextField.setText("0.00");
                totalInsulinTextField.setText("0.00");
                }
            } 
        }
        else{
            alarm.setAlarm("You do not have enough Insulin in your Reservoir");
        }
       
    }//GEN-LAST:event_insertInsulinActionPerformed

    private void totalInsulinTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_totalInsulinTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_totalInsulinTextFieldActionPerformed

    private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopButtonActionPerformed
        // TODO add your handling code here:
        Alarm alarm = new Alarm();
        InitSip initSip = new InitSip();
       
        int res = alarm.confirm("Are you sure you want to Stop the Insulin Pump?");
        if (res == 0){
            if(bolusInsulinThread != null){
                if(bolusInsulinThread.isAlive()){
                    bolusInsulinRunnable.terminate();
                    try{
                    bolusInsulinThread.join();
                    }catch(InterruptedException e){
                        SipLogger.Instance().debug("BolusInsulinThread was iterrupted.");
                    }
                }
            }
            if(basalThread != null){
                if(basalThread.isAlive()){
                    basalInsulinRunnable.terminate();
                    try{
                        basalThread.join();
                    }catch(InterruptedException e){
                        SipLogger.Instance().debug("basalThread was iterrupted.");
                    }
                }
            }
            initSip.writeCurrentValues();
            System.exit(1);
            //this.dispose();
            
            
        }
        
    }//GEN-LAST:event_stopButtonActionPerformed

    private void profileNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_profileNameTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_profileNameTextFieldActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        // TODO add your handling code here:
        // asks the user for confirmation
        Alarm alarm = new Alarm(); 
        //Object count = profileNameComboBox.getItemAt(profileNameComboBox.getSelectedIndex());
        String selectedItem;
        selectedItem = (String)profileNameComboBox.getSelectedItem();
        String activeProfile = (String)activeBasalTextField.getText();
        System.out.println(activeProfile);
        System.out.println(selectedItem);
        System.out.println(selectedItem.length());
        System.out.println(activeProfile.length());
        
        if(selectedItem.equals(""))//null )
        {
            alarm.setAlarm("There are no profiles to delete.");
        }
        else if(selectedItem.equals(activeProfile)){
            alarm.setAlarm("This Basal Profle is currently active. Unable to delete.");
        }
        
        else
        {
            
            int result = alarm.confirm("Are you sure you want to delete this Basal Profile?");
            if(result == 0) // if user presses YES
            {
                // removes the profile from config file
                    mBasalProfileManager.removeProfile(profileNameComboBox.getSelectedItem().toString());
            }

            //reads it agian from the config file 
                    mBasalProfileManager.readBasalProfiles();
                    List<String> profileNames = new ArrayList();
                    profileNames = mBasalProfileManager.getBasalProfileNames(); 
                    String[] profileNamesAsStrings = new String[profileNames.size()];
                    for(int i=0 ; i< profileNames.size();i++ )
                    {
                        profileNamesAsStrings[i] = profileNames.get(i);
                    }
            //String[] profileNames = mBasalProfileManager.getBasalProfileNames();        
            profileNameComboBox.setModel(new javax.swing.DefaultComboBoxModel(profileNamesAsStrings));
        }   

        
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void testButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testButtonActionPerformed
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel,"testScreen");
    }//GEN-LAST:event_testButtonActionPerformed

    private void testButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_testButtonKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_testButtonKeyPressed

    private void backTestButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backTestButtonActionPerformed
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel,"mainScreen");
        
    }//GEN-LAST:event_backTestButtonActionPerformed

    private void setInsulinButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setInsulinButtonActionPerformed
        // TODO add your handling code here:
        InitSip.totalInsulinAmount = new Double(String.valueOf(insulinTestTextField.getText()));
    }//GEN-LAST:event_setInsulinButtonActionPerformed

    private void setBatteryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setBatteryButtonActionPerformed
        // TODO add your handling code here:
        InitSip.BatteryLevel = new Double(String.valueOf(batteryTestTextField.getText()));
    }//GEN-LAST:event_setBatteryButtonActionPerformed

    private void setBubblesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setBubblesButtonActionPerformed
        // TODO add your handling code here:
        String text = bubblesTestTextField.getText();
        System.out.println(text);
        
        if("false".equals(text)){
            CheckStatus.bubbles = false;
        }
        else if("true".equals(text)){
            
            CheckStatus.bubbles = true;
        }
        else{
            System.out.println("Input not valid");
        }
     
       
        //InitSip.bubbles = new Boolean(String.valueOf(bubblesTestTextField.getText()));
        
    }//GEN-LAST:event_setBubblesButtonActionPerformed

    private void pauseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pauseButtonActionPerformed
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel,"mainScreen");
        
        
        
        if( pauseButton.getText()== "PAUSE")
        {
          //  basalInsulinRunnable.terminate();
            bolusButton.setEnabled(false);
            basalButton.setEnabled(false);
            pauseButton.setText("RESUME");
            if(basalThread != null){
                if(basalThread.isAlive()){
                    basalInsulinRunnable.mysuspend();
                }
            }
        }
        else if(pauseButton.getText() == "RESUME"){
            pauseButton.setText("PAUSE");
            bolusButton.setEnabled(true);
            basalButton.setEnabled(true);
            if(basalThread != null){
                if(basalThread.isAlive()){
                    basalInsulinRunnable.myresume();
                }
            }
        }
    }//GEN-LAST:event_pauseButtonActionPerformed

    private void cancelBolusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBolusButtonActionPerformed
        // TODO add your handling code here:
        Alarm alarm = new Alarm();
        insertInsulin.setEnabled(true);
        if(bolusInsulinThread != null){
            if(bolusInsulinThread.isAlive()){
               bolusInsulinRunnable.terminate();
               /*try{
                    bolusInsulinThread.;
                }catch(InterruptedException e){
                    SipLogger.Instance().debug("basalThread was iterrupted.");
                }
                */ 
            }
        }
        bolusTextArea.append("Bolus might not have been given completly. Please check History!");
    }//GEN-LAST:event_cancelBolusButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SimpleInsulinPumpUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SimpleInsulinPumpUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SimpleInsulinPumpUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SimpleInsulinPumpUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SimpleInsulinPumpUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton activateBasalActivationButton;
    private javax.swing.JLabel activeBasalLabel;
    private javax.swing.JTextField activeBasalTextField;
    private javax.swing.JButton addButton1;
    private javax.swing.JButton addButton2;
    private javax.swing.JButton addProfileButton;
    private javax.swing.JButton backBasalActivationButton;
    private javax.swing.JButton backBasalButton;
    private javax.swing.JButton backBolusButton;
    private javax.swing.JButton backHistoryButton;
    private javax.swing.JButton backTestButton;
    private javax.swing.JButton backVerificationButton;
    private javax.swing.JLabel basalActivationLabel;
    private javax.swing.JPanel basalActivationScreen;
    private javax.swing.JButton basalButton;
    private javax.swing.JLabel basalLabel;
    private javax.swing.JPanel basalScreen;
    private javax.swing.JLabel batteryLabel;
    private javax.swing.JTextField batteryTestTextField;
    private javax.swing.JButton bolusButton;
    private javax.swing.JPanel bolusScreen;
    private javax.swing.JScrollPane bolusScrollPane;
    public javax.swing.JTextArea bolusTextArea;
    private javax.swing.JTextField bubblesTestTextField;
    private javax.swing.JButton cancelBolusButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JSpinner carbInputSpinner;
    private javax.swing.JLabel carbLabel;
    private javax.swing.JButton clearBolusButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JComboBox foodComboBox;
    private javax.swing.JLabel foodLabel;
    private javax.swing.JLabel gLabel;
    private javax.swing.JButton historyButton;
    private javax.swing.JLabel historyLabel;
    private javax.swing.JPanel historyScreen;
    private javax.swing.JScrollPane historyScrollPane;
    private javax.swing.JTextArea historyTextArea;
    private javax.swing.JLabel incorrectMessageLabel;
    private javax.swing.JButton insertInsulin;
    private javax.swing.JSpinner insulinAmountSpinner;
    private javax.swing.JLabel insulinLabel;
    private javax.swing.JTextField insulinTestTextField;
    private javax.swing.JTextField insulinTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel labelBolus;
    private javax.swing.JPanel lowerPanel;
    private javax.swing.JLabel mainMenuLabel;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JPanel mainScreen;
    private javax.swing.JButton nextButton;
    private javax.swing.JButton okBolusButton;
    private javax.swing.JButton okVerificationButton;
    private javax.swing.JButton pauseButton;
    private javax.swing.JLabel pinLabel;
    private javax.swing.JLabel profileNameBasalActivationLabel;
    private javax.swing.JComboBox profileNameComboBox;
    private javax.swing.JLabel profileNameLabel;
    private javax.swing.JTextField profileNameTextField;
    private javax.swing.JButton saveBasalButton;
    private javax.swing.JButton setBatteryButton;
    private javax.swing.JButton setBubblesButton;
    private javax.swing.JButton setInsulinButton;
    private javax.swing.JButton stopButton;
    private javax.swing.JComboBox subFoodComboBox;
    private javax.swing.JButton testButton;
    private javax.swing.JLabel testLabel;
    private javax.swing.JPanel testScreen;
    private javax.swing.JLabel timeFrameLabel;
    private javax.swing.JLabel timeLabel;
    private javax.swing.JLabel totalInsulinLabel;
    private javax.swing.JTextField totalInsulinTextField;
    private javax.swing.JLabel totalLabel;
    private javax.swing.JTextField totalTextField;
    private javax.swing.JLabel unitsOfInsulinLabel;
    private javax.swing.JPanel upperPanel;
    private javax.swing.JPasswordField verificationPassword;
    private javax.swing.JPanel verificationScreen;
    // End of variables declaration//GEN-END:variables
}
